<?php
namespace Home\Controller;
use Think\Controller;
use Think\AjaxPage;
use Think\Page;
class LoginController extends Controller {
    public function reg(){
		$username = I('username')?I('username'):'';
		if(!$username){
			exit(json_encode(array('code'=>100,'msg'=>'请输入手机号或邮箱','jn_msg'=>'携帯番号やメールアドレスを入力してください。')));
		}
		$password = I('password')?I('password'):'';
		if(!$password){
			exit(json_encode(array('code'=>100,'msg'=>'请输入密码','jn_msg'=>'暗証番号を入力してください')));
		}
		$code = I('code')?I('code'):'';
		$info = M('news')->where(array('title'=>$code))->field('id,number,use_number')->find();
		if(!$info['id']){
			exit(json_encode(array('code'=>100,'msg'=>'邀请码无效','jn_msg'=>'招待コードが無効です')));
		}else if($info['number']<=$info['use_number']){
			exit(json_encode(array('code'=>100,'msg'=>'邀请码已使用','jn_msg'=>'招待コードは既に使用中です')));
		}
		$type_id = I('type_id')?I('type_id'):'';
		$realname = I('realname')?I('realname'):'';
		$address = I('address')?I('address'):'';
		$mobile = I('mobile')?I('mobile'):'';
		$weixin = I('weixin')?I('weixin'):'';
		$company = I('company')?I('company'):'';
		$email = I('email')?I('email'):'';
		$lineid = I('lineid')?I('lineid'):'';
		$mod = M('users');
		$check_user = $mod->where(array('username'=>$username))->count();		
		if($check_user){
			exit(json_encode(array('code'=>100,'msg'=>'用户已存在','jn_msg'=>'ユーザーは既に存在します')));
		}
		$goods_num = M('config')->where(array('id'=>1))->getField('value');
		$data = array(
			'code'=>$code,
			'username'=>$username,
			'password'=>md5($password),
			'type_id'=>$type_id,
			'realname'=>$realname,
			'address'=>$address,
			'mobile'=>$mobile,
			'weixin'=>$weixin,
			'company'=>$company,
			'email'=>$email,
			'lineid'=>$lineid,
			'goods_num'=>$goods_num,
			'token'=>md5($username.'mishangfeng'),
			'reg_time'=>time()
		);	
		$result = $mod->add($data);
		if($result){
			$data['id'] = $result;
            M('news')->where(array('id'=>$info['id']))->setInc('use_number',1); 
			exit(json_encode(array('code'=>200,'data'=>$data)));			
		}else{
			exit(json_encode(array('code'=>100,'msg'=>'注册失败','jn_msg'=>'登録に失敗する')));
		}
	}
	function tlogin(){
		$openid = I('openid')?I('openid'):'';
		$realname = I('realname')?I('realname'):'';
		$head_img = I('head_img')?I('head_img'):'';
		$mod = M('users');
		$time = time();
		$info = $mod->where(array('openid'=>$openid))->find();
		if(!$info['id']){
			$goods_num = M('config')->where(array('id'=>1))->getField('value');
			$username = 'hhf_'.$time.rand(1000,9999);
			$data = array(
				'openid'=>$openid,
				'realname'=>$realname,
				'type_id'=>2,
				'head_img'=>$head_img,
				'goods_num'=>$goods_num,
				'reg_time'=>$time
			);	
			$result = $mod->add($data);
			if($result){
				$data['username'] = $result;
				$data['id'] = $result;
				$mod->where(array('id'=>$result))->save(array('username'=>$result,'token'=>md5($result.'mishangfeng')));
				$data['token']=md5($result.'mishangfeng');
				exit(json_encode(array('code'=>200,'data'=>$data,'number'=>1)));			
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'注册失败','jn_msg'=>'登録に失敗する')));
			}
		}else{
			$data = array(
				'realname'=>$realname,
				'head_img'=>$head_img,
				'up_time'=>$time,
				'type_id'=>2,
				'token'=>md5($info['username'].'mishangfeng'),
			);	
			$result = $mod->where(array('id'=>$info['id']))->save($data);
			
			exit(json_encode(array('code'=>200,'data'=>$info,'number'=>2)));	
		}
	}
	function check(){
		$username = I('username')?I('username'):'';
		if(!$username){
			exit(json_encode(array('code'=>100,'msg'=>'请输入手机号或邮箱','jn_msg'=>'携帯番号やメールアドレスを入力してください。')));
		}
		$mod = M('users');
		$check_user = $mod->where(array('username'=>$username))->count();		
		if($check_user){
			exit(json_encode(array('code'=>100,'msg'=>'用户已存在','jn_msg'=>'ユーザーは既に存在します')));
		}else{
			$code = I('code')?I('code'):'';
			$type_id = I('type_id')?I('type_id'):'';
			$device_id = I('device_id')?I('device_id'):'';
			$now_time = strtotime(date('Y-m-d'));
			$news_number = M('news_log')->where(array('add_time'=>array('gt',$now_time,'device_id'=>$device_id)))->count();
			if($news_number<5){
				$info = M('news')->where(array('title'=>$code))->field('id,cat_id,number,use_number')->find();			
				if(!$info['id'] || $type_id!=$info['cat_id']){
					M('news_log')->add(array('device_id'=>$device_id,'add_time'=>time()));
					exit(json_encode(array('code'=>100,'msg'=>'邀请码已输错'.($news_number+1).'次','jn_msg'=>'招待コードが間違って'.($news_number+1).'回入力されました')));
				}else if($info['number']<=$info['use_number']){
					exit(json_encode(array('code'=>100,'msg'=>'邀请码已使用','jn_msg'=>'招待コードは既に使用中です')));
				}
				exit(json_encode(array('code'=>200,'num'=>rand(1000,9999))));				
			}else{
				exit(json_encode(array('code'=>100,'msg'=>'邀请码输错5次,请24小时后才可以进行操作','jn_msg'=>'招待コードが間違って5回入力されました。操作するまでに24時間お待ちください。')));
			}				
		}
	}
	
	function check_user(){
		$username = I('username')?I('username'):'';
		if(!$username){
			exit(json_encode(array('code'=>100,'msg'=>'请输入手机号或邮箱','jn_msg'=>'携帯番号やメールアドレスを入力してください。')));
		}
		$mod = M('users');
		$check_user = $mod->where(array('username'=>$username))->getField('id');		
		if($check_user){
			exit(json_encode(array('code'=>200,'data'=>$id)));	
		}else{
			exit(json_encode(array('code'=>100,'cn'=>'该手机号或邮箱不存在','jn'=>'電話番号または電子メールが存在しません')));					
		}
	}
	function pwd(){
		$username = I('username')?I('username'):'';
		if(!$username){
			exit(json_encode(array('code'=>100,'msg'=>'请输入手机号或邮箱','jn_msg'=>'携帯番号やメールアドレスを入力してください。')));
		}
		$uid = I('uid')?I('uid'):'';
		$password = I('password')?I('password'):'';
		$mod = M('users');
		$check_user = $mod->where(array('username'=>$username))->getField('id');		
		if($uid==$check_user){
			$data['token'] = '';
			$data['password']=md5($password);
			$data['up_time'] = time();
			$result = $mod->where(array('id'=>$check_user))->save($data);
			exit(json_encode(array('code'=>200,'data'=>$id)));	
		}else{
			exit(json_encode(array('code'=>100,'cn'=>'系统ID有误','jn'=>'不正なシステムID')));					
		}
	}
	function login(){
		$did = I('did')?I('did'):'';
		$username = I('username')?I('username'):'';
		if(!$username){
			exit(json_encode(array('code'=>100,'msg'=>'请输入手机号或邮箱','jn_msg'=>'携帯番号やメールアドレスを入力してください。')));
		}
		$password = I('password')?I('password'):'';
		if(!$password){
			exit(json_encode(array('code'=>100,'msg'=>'请输入密码','jn_msg'=>'暗証番号を入力してください')));
		}
		$mod = M('users');
		$user = $mod->where(array('username'=>$username))->find();
		if(!$user['id']){
			exit(json_encode(array('code'=>100,'msg'=>'用户不存在','jn_msg'=>'ユーザーは存在しません')));			
		}
		if($user['password']!=md5($password)){
			exit(json_encode(array('code'=>100,'msg'=>'密码不正确','jn_msg'=>'間違ったパスワード')));	
		}
		if(!$user['status']){
			exit(json_encode(array('code'=>100,'msg'=>'您的账号已被冻结，请联系管理员。','jn_msg'=>'あなたのアカウントは凍結されました。管理者に連絡してください。')));	
		}
		if($user['head_img']){
			if(!strpos($user['head_img'],'http')){
				$user['head_img'] = C('WEB_URL').$user['head_img'];
			}
		}
		$user['token'] = md5($user['username'].'mishangfeng');
		$result = $mod->where(array('id'=>$user['id']))->save(array('token'=>$user['token']));
		exit(json_encode(array('code'=>200,'data'=>$user)));	
	}
}